package jp.co.sharp.android.parents.kidsguard.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArraySet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jp.co.sharp.android.parents.kidsguard.R;

/**
 * 曜日を設定する
 * 保存  ふっきこーど 200 , あとずさりする  ふっきこーど 500
 */
public class PeriodSettingWeekActivity extends Activity implements OnCheckedChangeListener{
    private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();

    private CheckBox SUNDAY;
    private CheckBox MONDAY;
    private CheckBox TUESDAY;
    private CheckBox WEDNESDAY;
    private CheckBox THURSDAY;
    private CheckBox FRIDAY;
    private CheckBox SATURDAY;
    String position;

    private Set<String> like = new HashSet<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_period_setting_week);
        like.clear();
        backView();
        initCheckBox();
        // 获取控件
        SUNDAY = (CheckBox) findViewById(R.id.checkbox1);
        MONDAY = (CheckBox) findViewById(R.id.checkbox2);
        TUESDAY = (CheckBox) findViewById(R.id.checkbox3);
        WEDNESDAY = (CheckBox) findViewById(R.id.checkbox4);
        THURSDAY = (CheckBox) findViewById(R.id.checkbox5);
        FRIDAY = (CheckBox) findViewById(R.id.checkbox6);
        SATURDAY = (CheckBox) findViewById(R.id.checkbox7);

        // 绑定事件
        SUNDAY.setOnCheckedChangeListener(this);
        MONDAY.setOnCheckedChangeListener(this);
        TUESDAY.setOnCheckedChangeListener(this);
        WEDNESDAY.setOnCheckedChangeListener(this);
        THURSDAY.setOnCheckedChangeListener(this);
        FRIDAY.setOnCheckedChangeListener(this);
        SATURDAY.setOnCheckedChangeListener(this);
        // 获取勾选状态
        Intent intent = getIntent();
        int repetition = intent.getIntExtra("repetition",0);
        position =intent.getStringExtra("position");
        if(repetition>0){
            initRepetition(repetition);
        }
        initSubmit();
    }
    /**
     * 复选框选中与不选中的事件
     */
    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
        switch (checkBox.getId()) {
            case R.id.checkbox1:
                if (checked) {// 选中星期日
                    like.add( "40");
                } else {
                    like.remove("40");
                }
                break;
            case R.id.checkbox2:// 选中星期一
                if (checked) {
                    like.add( "01");
                } else {
                    like.remove("01");
                }
                break;
            case R.id.checkbox3:// 星期二
                if (checked) {
                    like.add( "02");
                } else {
                    like.remove("02");
                }
                break;
            case R.id.checkbox4:
                if (checked) {// 选中星期三
                    like.add( "04");
                } else {
                    like.remove("04");
                }
                break;
            case R.id.checkbox5:
                if (checked) {// 选中星期四
                    like.add( "08");
                } else {
                    like.remove("08");
                }
                break;
            case R.id.checkbox6:
                if (checked) {// 选中星期五
                    like.add( "10");
                } else {
                    like.remove("10");
                }
                break;
            case R.id.checkbox7:
                if (checked) {// 选中星期六
                    like.add( "20");
                } else {
                    like.remove("20");
                }
                break;
        }
    }
    public void backView(){
        ImageView imageView = findViewById(R.id.backView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(PeriodSettingWeekActivity.this,PeriodSettingActivity.class);
//                startActivity(intent);
                setResult(500);// 500代表不是点击的保存
                PeriodSettingWeekActivity.this.finish();
            }
        });


    }

    public void initCheckBox(){
        for(int i=1;i<=7;i++){
            int viewId = getResources().getIdentifier("checkbox"+i, "id", getPackageName());
            CheckBox checkBox = findViewById(viewId);
            checkBoxList.add(checkBox);
        }

    }

    public void initSubmit(){
        ImageView imageView = findViewById(R.id.submit);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                int a=getRepetitionHex16Int();

                intent.putExtra("repetition",getRepetitionHex16Int());//返回值
                intent.putExtra("position",position);//返回值
                setResult(200,intent);//200代表成功
                finish();
            }
        });
    }

    public Integer getRepetitionHex16Int() {
        int sum=0;
       for (String string:like){
           sum+=Integer.parseInt(string, 16);
       }
       return  sum;
    }
    public static Set<String> repetitionToHex16String(Integer repetition) {
            Set<String>   like=new HashSet<>();
            String repetitionHex16=Integer.toHexString(repetition);
            char[] split = repetitionHex16.toCharArray();
            switch (split[split.length - 1]) {
                case '1':
                    like.add("01");
                    break;
                case '2':
                    like.add("02");
                    break;
                case '4':
                    like.add("04");
                    break;
                case '8':
                    like.add("08");
                    break;
                case '3':
                    like.add("01");
                    like.add("02");
                    break;
                case '7':
                    like.add("01");
                    like.add("02");
                    like.add("04");
                    break;
                case 'f':
                    like.add("01");
                    like.add("02");
                    like.add("04");
                    like.add("08");
                    break;
                case '6':
                    like.add("02");
                    like.add("04");
                    break;
                case 'e':
                    like.add("02");
                    like.add("04");
                    like.add("08");
                    break;
                case 'c':
                    like.add("04");
                    like.add("08");
                    break;
            }
            if (split.length > 1) {
                switch (split[split.length - 2]) {
                    case '1':
                        like.add("10");
                        break;
                    case '2':
                        like.add("20");
                        break;
                    case '4':
                        like.add("40");
                        break;
                    case '3':
                        like.add("10");
                        like.add("20");
                        break;
                    case '7':
                        like.add("10");
                        like.add("20");
                        like.add("40");
                        break;
                    case '6':
                        like.add("20");
                        like.add("40");
                        break;
                }
            }
            return like;
    }
    private   Set<String> initRepetition(Integer repetition) {
        String repetitionHex16=Integer.toHexString(repetition);
        char[] split = repetitionHex16.toCharArray();
        switch (split[split.length - 1]) {
            case '1':
                MONDAY.setChecked(true);
                like.add("01");
                break;
            case '2':
                TUESDAY.setChecked(true);
                like.add("02");
                break;
            case '4':
                WEDNESDAY.setChecked(true);
                like.add("04");
                break;
            case '8':
                THURSDAY.setChecked(true);
                like.add("08");
                break;
            case '3':
                MONDAY.setChecked(true);
                TUESDAY.setChecked(true);
                like.add("01");
                like.add("02");
                break;
            case '7':
                MONDAY.setChecked(true);
                TUESDAY.setChecked(true);
                WEDNESDAY.setChecked(true);
                like.add("01");
                like.add("02");
                like.add("04");
                break;
            case 'f':
                MONDAY.setChecked(true);
                TUESDAY.setChecked(true);
                WEDNESDAY.setChecked(true);
                THURSDAY.setChecked(true);
                like.add("01");
                like.add("02");
                like.add("04");
                like.add("08");
                break;
            case '6':
                TUESDAY.setChecked(true);
                WEDNESDAY.setChecked(true);
                like.add("02");
                like.add("04");
                break;
            case 'e':
                TUESDAY.setChecked(true);
                WEDNESDAY.setChecked(true);
                THURSDAY.setChecked(true);
                like.add("02");
                like.add("04");
                like.add("08");
                break;
            case 'c':
                WEDNESDAY.setChecked(true);
                THURSDAY.setChecked(true);
                like.add("04");
                like.add("08");
                break;
        }
        if (split.length > 1) {
            switch (split[split.length - 2]) {
                case '1':
                    FRIDAY.setChecked(true);
                    like.add("10");
                    break;
                case '2':
                    SATURDAY.setChecked(true);
                    like.add("20");
                    break;
                case '4':
                    SUNDAY.setChecked(true);
                    like.add("40");
                    break;
                case '3':
                    FRIDAY.setChecked(true);
                    SATURDAY.setChecked(true);
                    like.add("10");
                    like.add("20");
                    break;
                case '7':
                    FRIDAY.setChecked(true);
                    SATURDAY.setChecked(true);
                    SUNDAY.setChecked(true);
                    like.add("10");
                    like.add("20");
                    like.add("40");
                    break;
                case '6':
                    SATURDAY.setChecked(true);
                    SUNDAY.setChecked(true);
                    like.add("20");
                    like.add("40");
                    break;
            }
        }
        return like;
    }
}
